package pencli.view;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

public class JSONResponseServlet extends HttpServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		System.out.println("제이슨 호출");

		Object obj= request.getAttribute("res_data");
		String responseMsg =null;
	
		
		
		if(obj instanceof ArrayList){
			ArrayList resData = (ArrayList)request.getAttribute("res_data");
			JSONArray jarr = new JSONArray(resData);
			responseMsg= jarr.toString();
		}else{
			JSONObject jobj= new JSONObject(obj);
			responseMsg = jobj.toString();
		}
		
			response.setContentType("text/html;charset=utf-8");
			response.setHeader("cache-control", "no-cache");
			PrintWriter out = response.getWriter();
			System.out.println(responseMsg);
			out.print(responseMsg);
					
	}

}
